﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.Linq;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Logging;
using ScrumTable.DL.Data.Common.Serialization;
using ScrumTable.DL.Data.Generic;
using ScrumTable.DL.Data.Generic.Cache;
using ScrumTable.DL.Data.Generic.Services;
using ScrumTable.DL.Data.Local.Properties;

#endregion

namespace ScrumTable.DL.Data.Local.Services
{
    /// <summary>
    /// This class is used to synchronize data from the data layer to the
    /// XML data storage.
    /// </summary>
    internal class DataSaveProcess
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly CacheDataChanges _changes;
        private readonly GenericDataContextBase _dataContext;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DataSaveProcess instance, no public constructor
        /// needed.
        /// </summary>
        /// <param name="dataContext">Specifies the data context which contains the underlying data driver root object.</param>
        /// <param name="changes">Specifies the change set to synchronize.</param>
        internal DataSaveProcess(GenericDataContextBase dataContext, CacheDataChanges changes)
        {
            PreCondition.AssertNotNull(dataContext, "dataContext");
            PreCondition.AssertNotNull(changes, "changes");

            _dataContext = dataContext;
            _changes = changes;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Synchronizes the changes with the team foundation server and
        /// sets the accepted flag if the change has been successfully
        /// stored back to the back end system.
        /// </summary>
        internal void SyncChanges()
        {
            Exception serializationError = null;

            if (_changes.ChangedElementCount > 0)
            {
                try
                {
                    _dataContext.Serialize();
                }
                catch (Exception e)
                {
                    serializationError = e;
                    Logger<DataSaveProcess>.Error(Resources.ExcDataSvProcFailed, e);
                }
            }

            if (serializationError == null)
            {
                _changes.RemoveDirtyFlags();
                _changes.MarkChangesAs(CacheSavePairState.Saved);
            }
            else
            {
                _changes.MarkChangesAsError(serializationError);
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}

